/*
 * Created on 2005-7-11
 *
 */
package com.et114.components.ldap4oj.core;

import java.util.ArrayList;
import java.util.Collection;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchResult;


public class AttributeHelper {
    

    private AttributeHelper() {
    }

    public static boolean isInstanceOf(SearchResult searchResult,
            String className) throws NamingException {
        return getAttributeAsStringCollection(searchResult, "objectclass")
                .contains(className);
    }

    public static Collection getAttributeAsStringCollection(
            SearchResult searchResult, String attributeName)
            throws NamingException {
        return getAttributeAsStringCollection(searchResult.getAttributes(),
                attributeName);
    }

    public static Collection getAttributeAsStringCollection(
            Attributes attributes, String attributeName) throws NamingException {
        Collection result = new ArrayList();
        Attribute attribute = attributes.get(attributeName);

        if (attribute != null) {
            NamingEnumeration namingEnumeration = attribute.getAll();
            try {
                while (namingEnumeration.hasMore()) {
                    result.add(namingEnumeration.next());
                }
            } finally {
                namingEnumeration.close();
            }
        }

        return result;
    }

    public static String[] getAttributeAsStringArray(SearchResult searchResult,
            String attributeName) throws NamingException {
        Collection result = getAttributeAsStringCollection(searchResult,
                attributeName);
        return (String[]) result.toArray(new String[result.size()]);
    }

    public static String[] getAttributeAsStringArray(Attributes attributes,
            String attributeName) throws NamingException {
        Collection result = getAttributeAsStringCollection(attributes,
                attributeName);
        return (String[]) result.toArray(new String[result.size()]);
    }

    public static String getAttributeAsString(SearchResult searchResult,
            String attributeName) throws NamingException {
        return getAttributeAsString(searchResult.getAttributes(), attributeName);
    }

    public static String getAttributeAsString(Attributes attributes,
            String attributeName) throws NamingException {
        Attribute attribute = attributes.get(attributeName);
        if (attribute == null) {
            return null;
        }
        return (String) attribute.get();
    }

    public static String getAttributeAsNonNullString(SearchResult searchResult,
            String attributeName) throws NamingException {
        return getAttributeAsNonNullString(searchResult.getAttributes(),
                attributeName);
    }

    public static String getAttributeAsNonNullString(Attributes attributes,
            String attributeName) throws NamingException {
        String result = getAttributeAsString(attributes, attributeName);
        if (result == null) {
            result = "";
        }
        return result;
    }


}
